# coding=utf-8
import datetime
import json
import base64
import pymongo
import random

from django.db import transaction
from django.http import JsonResponse
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import permissions
from rest_framework import status
from rest_framework import viewsets, mixins
from plantform.serializers import *
from plantform.models import *
from plantform.tasks import spark_async_submit
from rest_framework.response import Response
from plantform.task_config import TASK_CONFIG
from rest_framework.decorators import action

from report.models import Report
from report.serializers import ReportSerializers

from kd_server.settings import BASE_DIR
from utils import logger
from utils.pagination import SmallResultsSetPagination


class PlantformDataSourceViewSet(viewsets.ModelViewSet):
    """
    输入源配置类
    """
    permission_classes = (permissions.IsAuthenticated,)
    pagination_class = SmallResultsSetPagination
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('data_source_status',)

    def get_serializer_class(self):
        if self.action == 'create':
            return DataSourceCreateSerializers
        if self.action == 'list' or self.action == 'retrieve':
            return DataSourceListSerializers
        return DataSourceUpdateSerializers

    def get_queryset(self):
        if not self.request.user:
            logger.debug('用户没有登录')
            return DataSource.objects.none()
        return DataSource.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        with transaction.atomic():
            logger.debug('atomic 执行保存操作')
            serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        with transaction.atomic():
            logger.debug("atomic 执行更新操作")
            serializer.save(user=self.request.user)


class PlantformOutDataSourceViewSet(viewsets.ModelViewSet):
    """
    拖拽平台的输出源配置类
    """
    permission_classes = (permissions.IsAuthenticated,)
    pagination_class = SmallResultsSetPagination
    filter_backends = (DjangoFilterBackend,)

    def get_serializer_class(self):
        if self.action == 'create':
            return OutDataSourceCreateSerializers
        if self.action == 'list' or self.action == 'retrieve':
            return OutDataSourceListSerializers
        return OutDataSourceUpdateSerializers

    def get_queryset(self):
        if not self.request.user:
            logger.debug('用户没有登录')
            return OutDataSource.objects.none()
        return OutDataSource.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        with transaction.atomic():
            logger.debug('atomic 执行保存操作')
            serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        with transaction.atomic():
            logger.debug("atomic 执行更新操作")
            serializer.save(user=self.request.user)


class PlantformConfigViewSet(viewsets.ModelViewSet):
    """
    数据清洗以及数据特征转换和数据挖掘相关配置
    """
    permission_classes = (permissions.IsAuthenticated,)
    pagination_class = SmallResultsSetPagination
    filter_backends = (DjangoFilterBackend,)

    def get_serializer_class(self):
        if self.action == 'create':
            return PlantformConfigCreateSerializers
        if self.action == 'list' or self.action == 'retrieve':
            return PlantformConfigListSerializers
        return PlantformConfigUpdateSerializers

    def get_queryset(self):
        if not self.request.user:
            logger.debug('用户没有登录')
            return PlantformConfig.objects.none()
        return PlantformConfig.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        with transaction.atomic():
            logger.debug('atomic 执行保存操作')
            serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        with transaction.atomic():
            logger.debug("atomic 执行更新操作")
            serializer.save(user=self.request.user)


class FeaturesAndMLConfigViewSet(viewsets.GenericViewSet):
    permission_classes = (permissions.IsAuthenticated,)

    def get_serializer_class(self):
        return BaseSearilizers

    @action(['get'], url_path="get_ml_config", detail=False, url_name="get_feature_and_ml_config")
    def get_base_config(self, request):
        """
        返回默认的数据处理和算法配置
        :param request:
        :return:
        """
        result = {}
        result['ml_config'] = TASK_CONFIG['MLConf']
        result['feature_config'] = TASK_CONFIG['TransformConf']
        result['MLTypeList'] = ['LDA','TF_IDF','K_Means',"ALS","Word2Vector"],
        return JsonResponse(result, status=200)


class PlantformTaskViewSet(viewsets.ModelViewSet):
    """
    数据清洗任务相关
    """
    permission_classes = (permissions.IsAuthenticated,)
    pagination_class = SmallResultsSetPagination
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ("task_status",)

    def get_serializer_class(self):
        if self.action == 'create':
            return TaskCreateSerializers
        if self.action == 'list' or self.action == 'retrieve':
            return TaskListSerializers
        return TaskUpdateSerializers

    def get_queryset(self):
        if not self.request.user:
            logger.debug('用户没有登录')
            return Task.objects.none()
        return Task.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        with transaction.atomic():
            logger.debug('atomic 执行保存操作')
            # 这里来执行一下名字补充操作
            serializer.save(user=self.request.user)

    def perform_update(self, serializer):
        with transaction.atomic():
            logger.debug("atomic 执行更新操作")
            serializer.save(user=self.request.user)


class DocIndexViewSet(viewsets.GenericViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = DocIndexSerializers
    mongo_db = pymongo.MongoClient("mongodb://175.102.18.112:27018").OpenCollege_kw

    def get_all_index(self, request):
        try:
            if 'doc_type' in request.GET:
                doc_type = request.GET['doc_type']
            else:
                doc_type = 'edu'
            if 'keyword' in request.GET:
                keyword = request.GET['keyword']
                find_dict = {'keyword': keyword}
            else:
                find_dict = {}
            if 'page' in request.GET:
                page = int(request.GET['page'])
            else:
                page = 1
            results = []
            docs = self.mongo_db['art_' + doc_type].find(find_dict, {"_id": 0}).skip((page - 1) * 20).limit(20)
            for doc in docs:
                results.append(doc)
            return JsonResponse({'code': 200, "message": "获取成功", "data": results}, status=200)

        except Exception as e:
            return JsonResponse({'code': 400, "message": "获取参数失败" + str(e)}, status=400)


class TaskStartViewSet(viewsets.GenericViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = TaskStartSerializers

    def start_task(self, request):
        """
        启动任务接口
        :param request:
        :return:
        """
        try:
            task_id = request.data['task_id']
            if not task_id:
                return JsonResponse({'code': 400, "message": "获取参数失败，检查task_id"}, status=400)
            # 首先读取相关配置组成最终的配置参数
            task_detail = Task.objects.get(pk=task_id)
            self.serializer_class = TaskListSerializers
            detail_serializer = self.get_serializer(task_detail).data
            # 获取到配置参数后进行值替换：
            conf_set = TASK_CONFIG
            # app基本设置
            conf_set['AppSettingConf']['AppName'] = detail_serializer['ml_config']['app_name']
            conf_set['AppSettingConf']['SparkMaster'] = detail_serializer['ml_config']['spark_master']
            # 首先配置输入源
            conf_set['DataSourceInputConf']['DataSourceType'] = detail_serializer['data_source']['data_source_type']
            if detail_serializer['data_source']['data_source_type'] == "textFile":
                conf_set['DataSourceInputConf']['DataSourceFileType'] = detail_serializer['data_source'][
                    'txt_file_type']
                conf_set['DataSourceInputConf']['DataSourceLocation'] = detail_serializer['data_source'][
                    'txt_file_path']
                conf_set['DataSourceInputConf']['DataSourceDelimiter'] = ','
            if detail_serializer['data_source']['data_source_type'] == 'hdfs':
                conf_set['DataSourceInputConf']['DataSourceLocation'] = detail_serializer['data_source']['hdfs']
                conf_set['DataSourceInputConf']['DataSourceFileType'] = 'hdfs'
            if detail_serializer['data_source']['data_source_type'] == 'db':
                conf_set['DataSourceInputConf']['DataSourceDBUri'] = detail_serializer['data_source']['db_uri']
                conf_set['DataSourceInputConf']['DataSourceDBType'] = detail_serializer['data_source']['db_type']
                conf_set['DataSourceInputConf']['DataSourceHost'] = detail_serializer['data_source']['db_host']
                conf_set['DataSourceInputConf']['DataSourcePort'] = detail_serializer['data_source']['db_port']
                conf_set['DataSourceInputConf']['DataSourceUser'] = detail_serializer['data_source']['db_user']
                conf_set['DataSourceInputConf']['DataSourcePwd'] = detail_serializer['data_source']['db_pwd']
                conf_set['DataSourceInputConf']['DataSourceDBName'] = detail_serializer['data_source']['db_name']
                conf_set['DataSourceInputConf']['DataSourceDBTable'] = detail_serializer['data_source']['db_form']
                conf_set['DataSourceInputConf']['DataSourceDBFds'] = detail_serializer['data_source'][
                    'db_fields'].split(",")
            conf_set['DataSourceInputConf']['DataSourceLimitCount'] = detail_serializer['data_source']['limit_count']
            # 其次是数据清洗
            conf_set['MillConf']['ZHAble'] = detail_serializer['ml_config']['zh_support']
            conf_set['MillConf']['AllowNumber'] = detail_serializer['ml_config']['allow_number']
            conf_set['MillConf']['LimitCount'] = detail_serializer['ml_config']['split_limit_count']
            conf_set['MillConf']['FastStopWords'] = detail_serializer['ml_config']['fast_stop_words']
            # 再者是特征转换相关
            conf_set['TransformConf']['TransformType'] = detail_serializer['ml_config']['transform_type']
            conf_set['TransformConf']['InputCol'] = detail_serializer['ml_config']['input_col']
            conf_set['TransformConf']['OutPutCol'] = detail_serializer['ml_config']['output_col']
            # 在然后是算法选择相关
            conf_set['MLConf']['MLType'] = detail_serializer['ml_config']['ml_type']
            conf_set['MLConf'][detail_serializer['ml_config']['ml_type']] = conf_set['MLConf'][
                detail_serializer['ml_config']['ml_type']]
            # 然后就是结果输出的配置了
            conf_set['OutputConf']['OutputType'] = detail_serializer['output_data_source']['output_type']
            if detail_serializer['output_data_source']['output_type'] == "textFile":
                conf_set['OutputConf']['OutTextPath'] = BASE_DIR + "/data_files/" + detail_serializer['output_data_source']['text_path']
            if detail_serializer['output_data_source']['output_type'] == 'hdfs':
                conf_set['OutputConf']['OutHDFSPath'] = detail_serializer['output_data_source']['hdfs_path']
            if detail_serializer['output_data_source']['output_type'] == 'db':
                conf_set['OutputConf']['OutDataBaseType'] = detail_serializer['output_data_source']['database_type']
                conf_set['OutputConf']['OutDataBaseName'] = detail_serializer['output_data_source']['database_name']
                conf_set['OutputConf']['OutDataBaseHost'] = detail_serializer['output_data_source']['database_host']
                conf_set['OutputConf']['OutDataBasePort'] = detail_serializer['output_data_source']['database_port']
                conf_set['OutputConf']['OutDataBaseTable'] = detail_serializer['output_data_source']['database_table']
                conf_set['OutputConf']['OutDataBaseUser'] = detail_serializer['output_data_source']['database_user']
                conf_set['OutputConf']['OutDataBasePwd'] = detail_serializer['output_data_source']['database_pwd']
            # 组装配置完成，进行base64编码
            if 'MLTypeList' in conf_set['MLConf']:
                del conf_set['MLConf']['MLTypeList']
            conf_string = base64.b64encode(json.dumps(conf_set).encode('utf-8')).decode("utf-8")
            # 调起子进程执行shell命令提交任务
            celery_id = self.spark_cmd_exec(conf_string, task_id)
            # 将任务ID写入数据库中
            # task_detail['celery_id'] = celery_id
            # task_detail['task_status'] = 1
            task_detail.celery_id = celery_id
            task_detail.task_status = 1

            # 启动report任务
            try:
                report_ins = Report.objects.get(task_id=task_id)
                report_ins.report_status = 1
                report_ins.save()
            except Exception as e:
                pass

            # 任务信息保存
            task_detail.save()
            return JsonResponse({"code": 200, "message": "启动任务成功", "conf_string": conf_string}, status=200)
        except Exception as e:
            return JsonResponse({"code": 400, "message": "启动任务失败，失败原因: \n" + str(e)}, status=400)

    def spark_cmd_exec(self, conf_string, start_task_id):
        """
        异步执行任务，将任务添加到任务队列
        :param conf_string:
        :param start_task_id:
        :return:
        """
        # conf_string = """eyJBcHBTZXR0aW5nQ29uZiI6IHsiQXBwTmFtZSI6ICJrZF9zZXJ2ZXIiLCAiU3BhcmtNYXN0ZXIiOiAibG9jYWxbKl0ifSwgIkRhdGFTb3VyY2VJbnB1dENvbmYiOiB7IkRhdGFTb3VyY2VUeXBlIjogInRleHRGaWxlIiwgIkRhdGFTb3VyY2VGaWxlVHlwZSI6ICJjc3YiLCAiRGF0YVNvdXJjZUxvY2F0aW9uIjogIi9Vc2Vycy94aWFvanVuL0Rlc2t0b3AvXHU1ZjAwXHU1OTI3XHU0ZTJhXHU2MDI3XHU1MzE2XHU2M2E4XHU4MzUwXHU2MmE1XHU1NDRhL3VzZXJfcmVjb3JkLmNzdiIsICJEYXRhU291cmNlRGVsaW1pdGVyIjogIiwiLCAiRGF0YVNvdXJjZURCVXJpIjogIiIsICJEYXRhU291cmNlREJUeXBlIjogIm15c3FsIiwgIkRhdGFTb3VyY2VIb3N0IjogIjEyNy4wLjAuMSIsICJEYXRhU291cmNlUG9ydCI6IDMzMDYsICJEYXRhU291cmNlVXNlciI6ICJyb290IiwgIkRhdGFTb3VyY2VQd2QiOiAiIiwgIkRhdGFTb3VyY2VEQk5hbWUiOiAiIiwgIkRhdGFTb3VyY2VEQlRhYmxlIjogIiIsICJEYXRhU291cmNlREJGZHMiOiBbXSwgIkRhdGFTb3VyY2VMaW1pdENvdW50IjogMH0sICJNaWxsQ29uZiI6IHsiWkhBYmxlIjogdHJ1ZSwgIkFsbG93TnVtYmVyIjogdHJ1ZSwgIkxpbWl0Q291bnQiOiAyLCAiRmFzdFN0b3BXb3JkcyI6ICIifSwgIlRyYW5zZm9ybUNvbmYiOiB7IlRyYW5zZm9ybVR5cGUiOiAiQ291bnRWZWN0b3IiLCAiVHJhbnNmb3JtVHlwZUxpc3QiOiBbIkNvdW50VmVjdG9yIiwgIkhhc2hpbmdURiJdLCAiQ291bnRWZWN0b3IiOiB7IklucHV0Q29sIjogIndvcmRzIiwgIk91dFB1dENvbCI6ICJmZWF0dXJlIn0sICJIYXNoaW5nVEYiOiB7IklucHV0Q29sIjogIndvcmRzIiwgIk91dFB1dENvbCI6ICJmZWF0dXJlIn0sICJJbnB1dENvbCI6ICJzdHJpbmciLCAiT3V0UHV0Q29sIjogInN0cmluZyJ9LCAiTUxDb25mIjogeyJNTFR5cGUiOiAiQUxTIiwgIkxEQSI6IHsiayI6IDMsICJtYXhJdE51bSI6IDIwLCAib3B0aW1pemVyIjogImVtIiwgImRlc2NyaWJlVG9waWNzIjogMTB9LCAiVEZfSURGIjogeyJtaW5Eb2MiOiAyLCAiaW5wdXRDb2wiOiAiZmVhdHVyZXMiLCAib3V0cHV0Q29sIjogImlkZiJ9LCAiS19NZWFucyI6IHsiayI6IDEwLCAibWF4SXROdW0iOiAzMCwgInNlZWQiOiAxfSwgIkFMUyI6IHsiayI6IDEwLCAibWF4SXROdW0iOiAzMCwgInJlZ1BhcmFtIjogMC4wMSwgImltcGxpY2l0UHJlZnMiOiB0cnVlLCAidXNlckNvbCI6ICJpbmRpY2VzU2lkIiwgIml0ZW1Db2wiOiAiaW5kaWNlc0NpZCIsICJyYXRpbmdDb2wiOiAicmF0aW5nIn0sICJXb3JkMlZlY3RvciI6IHt9LCAiTUxQYXJhbXMiOiB7fX0sICJPdXRwdXRDb25mIjogeyJPdXRwdXRUeXBlIjogImNvbnNvbGUiLCAiT3V0VGV4dFBhdGgiOiAiIiwgIk91dERhdGFCYXNlVHlwZSI6ICIiLCAiT3V0RGF0YUJhc2VOYW1lIjogIiIsICJPdXREYXRhQmFzZUhvc3QiOiAiIiwgIk91dERhdGFCYXNlUG9ydCI6ICIiLCAiT3V0RGF0YUJhc2VUYWJsZSI6ICIiLCAiT3V0RGF0YUJhc2VGZHMiOiAiIiwgIk91dERhdGFCYXNlVXNlciI6ICIiLCAiT3V0RGF0YUJhc2VQd2QiOiAiIiwgIk91dEhERlNQYXRoIjogIiJ9fQ=="""
        # 异步去执行提交任务，并在执行完成后将任务状态置为成功或者失败
        task_info = spark_async_submit.delay(conf_string, start_task_id)
        return task_info.task_id

    def random_list(self, start, stop, length):
        if length >= 0:
            length = int(length)
        start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
        random_list = []
        for i in range(length):
            random_list.append(random.randint(start, stop))
        return random_list

    def get_random_data(self):
        """
        获取数据接口
        :return:
        """
        words = [{'name': '教育', 'value': 15523}, {'name': '教学', 'value': 6724},
                 {'name': '学习', 'value': 6654}, {'name': '社会', 'value': 6652},
                 {'name': '课程', 'value': 5897}, {'name': '学生', 'value': 5012},
                 {'name': '基础', 'value': 4500}, {'name': '培养', 'value': 4204},
                 {'name': '知识', 'value': 3754}, {'name': '管理', 'value': 3726},
                 {'name': '专业', 'value': 3634}, {'name': '工作', 'value': 3567},
                 {'name': '理论', 'value': 3367}, {'name': '体系', 'value': 3352},
                 {'name': '技术', 'value': 3256}, {'name': '经济', 'value': 3252},
                 {'name': '职业', 'value': 3250}, {'name': '质量', 'value': 2947},
                 {'name': '科学', 'value': 2822}, {'name': '高等教育', 'value': 2697},
                 {'name': '创新', 'value': 2405}, {'name': '应用', 'value': 2367},
                 {'name': '高校', 'value': 2351}, {'name': '文化', 'value': 2320},
                 {'name': '教师', 'value': 2237}, {'name': '随着', 'value': 2132},
                 {'name': '系统', 'value': 2101}, {'name': '中国', 'value': 2096},
                 {'name': '学校', 'value': 2050}, {'name': '生活', 'value': 1919},
                 {'name': '思想', 'value': 1871}, {'name': '培训', 'value': 1855},
                 {'name': '世界', 'value': 1814}, {'name': '设计', 'value': 1760},
                 {'name': '公开课', 'value': 1684}, {'name': '技能', 'value': 1645},
                 {'name': '网易', 'value': 1598}, {'name': '必须', 'value': 1559},
                 {'name': '院校', 'value': 1546}, {'name': '网络', 'value': 1534},
                 {'name': '历史', 'value': 1456}, {'name': '终身教育', 'value': 1437},
                 {'name': '开放', 'value': 1399}, {'name': '农村', 'value': 1357},
                 {'name': '继续', 'value': 1336}, {'name': '学科', 'value': 1279},
                 {'name': '学院', 'value': 1158}, {'name': '人们', 'value': 1144},
                 {'name': '帮助', 'value': 1116}, {'name': '高职', 'value': 1112},
                 {'name': '人才培养', 'value': 1074}, {'name': '健康', 'value': 1065},
                 {'name': '企业', 'value': 1031}, {'name': '工程', 'value': 1000}, {'name': '精神', 'value': 968},
                 {'name': '政府', 'value': 956}, {'name': '评价', 'value': 948}, {'name': '人类', 'value': 922},
                 {'name': '学员', 'value': 881}, {'name': '农民', 'value': 864}, {'name': '社区', 'value': 849},
                 {'name': '计算', 'value': 842}, {'name': '翻译', 'value': 832}, {'name': '使用', 'value': 807},
                 {'name': '考试', 'value': 798}, {'name': '课堂', 'value': 784}, {'name': '大学生', 'value': 739},
                 {'name': '原理', 'value': 733}, {'name': '作者', 'value': 705}, {'name': '政治', 'value': 702},
                 {'name': '医学', 'value': 694}, {'name': '全国', 'value': 679}, {'name': '语言', 'value': 672},
                 {'name': '审译', 'value': 671}, {'name': '美国', 'value': 668}, {'name': '社会主义', 'value': 641},
                 {'name': '生产', 'value': 640}, {'name': '数据', 'value': 636}, {'name': '教材', 'value': 633},
                 {'name': '就业', 'value': 626}, {'name': '英语', 'value': 617}, {'name': '自我', 'value': 614},
                 {'name': '和谐', 'value': 611}, {'name': '计算机', 'value': 611}, {'name': '艺术', 'value': 611},
                 {'name': '生命', 'value': 592}, {'name': '素养', 'value': 591}, {'name': '化学', 'value': 588},
                 {'name': '实验', 'value': 577}, {'name': '教育部', 'value': 567}, {'name': '地区', 'value': 556},
                 {'name': '农业', 'value': 535}, {'name': '城市', 'value': 523}, {'name': '人文', 'value': 503},
                 {'name': '人生', 'value': 495}, {'name': '数学', 'value': 485}, {'name': '群体', 'value': 477},
                 {'name': '教学管理', 'value': 474}, {'name': '普通高校', 'value': 473},
                 {'name': '招生', 'value': 457}, {'name': '故事', 'value': 440}, {'name': '素质教育', 'value': 438},
                 {'name': '文明', 'value': 428}, {'name': '演讲', 'value': 425}, {'name': '控制', 'value': 403},
                 {'name': '安全', 'value': 379}, {'name': '生物', 'value': 378}, {'name': '软件', 'value': 372},
                 {'name': '数字', 'value': 365}, {'name': '成绩', 'value': 364}, {'name': '文献', 'value': 338},
                 {'name': '物理', 'value': 322}, {'name': '文学', 'value': 313}, {'name': '作品', 'value': 309},
                 {'name': '体育', 'value': 306}, {'name': '临床', 'value': 299}, {'name': '材料', 'value': 296},
                 {'name': '创业', 'value': 284}, {'name': '论文', 'value': 282}, {'name': '纪录片', 'value': 257},
                 {'name': '力学', 'value': 255}, {'name': '阅读', 'value': 252}, {'name': '解析', 'value': 251},
                 {'name': '专业化', 'value': 249}, {'name': '青年', 'value': 245}, {'name': '建筑', 'value': 243},
                 {'name': '孩子', 'value': 235}, {'name': '公司', 'value': 233}, {'name': '音乐', 'value': 221},
                 {'name': '农民工', 'value': 221}, {'name': '老年', 'value': 211}, {'name': '移动', 'value': 202},
                 {'name': '行政', 'value': 201}, {'name': '高考', 'value': 200}, {'name': '护理', 'value': 194},
                 {'name': '投资', 'value': 192}, {'name': '人体', 'value': 192}, {'name': '中等职业', 'value': 186},
                 {'name': '考生', 'value': 178}, {'name': '解题', 'value': 174}, {'name': '女性', 'value': 173},
                 {'name': '员工', 'value': 167}, {'name': '动物', 'value': 165}, {'name': '会计', 'value': 159},
                 {'name': '儿童', 'value': 157}, {'name': '学籍', 'value': 157}, {'name': '写作', 'value': 151},
                 {'name': '德育', 'value': 147}, {'name': '工艺', 'value': 138}, {'name': '通信', 'value': 129},
                 {'name': '风险', 'value': 129}, {'name': '旅游', 'value': 125}, {'name': '命题', 'value': 124},
                 {'name': '档案管理', 'value': 119}, {'name': '语文', 'value': 114}, {'name': '教务', 'value': 113},
                 {'name': '家长', 'value': 108}, {'name': '校长', 'value': 106}, {'name': '程序设计', 'value': 103},
                 {'name': '场所', 'value': 100}, {'name': '植物', 'value': 96}, {'name': '摄影', 'value': 96},
                 {'name': '新生代', 'value': 90}, {'name': '视觉', 'value': 72}, {'name': '家庭教育', 'value': 70},
                 {'name': '药物', 'value': 67}, {'name': '中药', 'value': 44}, {'name': '单片机', 'value': 21},
                 {'name': 'ted', 'value': 20}]

        # 排名前20的
        word_cloud_data = random.sample(words[:50], 20)
        # 然后是一个柱状显示

        # 获取当前日期前一个月的日期标签
        d = datetime.datetime.now()
        date_list = []
        value_list = self.random_list(2, 150, 30)
        for i in range(0, 30):
            tmp = d + datetime.timedelta(days=-i)
            date_list.append(tmp.strftime("%Y-%m-%d"))
        bar_data = {
            "x_data": date_list,
            "y_data": value_list,
        }
        # 获取最大数据
        max_date = date_list[value_list.index(max(value_list))]
        max_value = max(value_list)
        sum_value = sum(value_list)
        average_value = sum_value / 30
        # 然后是折线图
        line_data_x = [word['name'] for word in word_cloud_data]
        line_data_y = [word['value'] for word in word_cloud_data]
        line_data = {
            "x_data": line_data_x,
            "y_data": line_data_y
        }
        # 获取折线图最大
        max_word = line_data_x[line_data_y.index(max(line_data_y))]
        max_word_value = max(line_data_y)
        # 再然后就是饼图
        selected_dict = {}
        for data in line_data_x:
            selected_dict[data] = True if random.randint(0, 10) < 3 else False
        pie_data = {
            "legendData": line_data_x,
            "selected": selected_dict,
            "seriesData": word_cloud_data
        }
        # selected_keywords
        select_keywords = []
        for key in selected_dict:
            if selected_dict[key]:
                select_keywords.append(key)
        select_keywords = ",".join(select_keywords)
        # relationship关系图
        relation_data = {
            "name": "远程教育",
            "children": [
                {
                    "name": "教育",
                    "children": [
                        {
                            "name": "管理",
                            "children": [
                                {"name": "职场", "value": 3938},
                                {"name": "经济", "value": 3812},
                                {"name": "插花", "value": 6714},
                                {"name": "老年人", "value": 1651},
                                {"name": "艺术", "value": 743},
                                {"name": "行为", "value": 254},
                                {"name": "大学生", "value": 2254},
                                {"name": "能力", "value": 1254},
                            ]
                        },
                        {
                            "name": "社会",
                            "children": [
                                {"name": "手机", "value": 2534},
                                {"name": "苹果", "value": 731},
                                {"name": "虚拟化", "value": 40},
                                {"name": "发展", "value": 3914},
                                {"name": "外贸", "value": 557},
                                {"name": "中国", "value": 574},
                                {"name": "信息化", "value": 416},
                                {"name": "就业", "value": 1116},
                                {"name": "理论", "value": 169},
                                {"name": "珠宝", "value": 716},
                            ]
                        },
                    ]
                },
                {
                    "name": "学习",
                    "children": [
                        {"name": "教育资源", "value": 710},
                        {"name": "学习", "value": 5842},
                        {
                            "name": "英语口语",
                            "children": [
                                {"name": "标准", "value": 4983},
                                {"name": "发音", "value": 2047},
                                {"name": "单词", "value": 1375},
                                {"name": "美国", "value": 746},
                            ]
                        },
                        {
                            "name": "课程",
                            "children": [
                                {"name": "军事", "value": 154},
                                {"name": "茶艺", "value": 1512},
                                {"name": "文化", "value": 541},
                                {"name": "教育", "value": 7074},
                                {"name": "陶瓷", "value": 681},
                                {"name": "国防教育", "value": 2510},
                                {"name": "优质", "value": 1250},
                                {
                                    "name": "医学健康",
                                    "children": [
                                        {"name": "教学", "value": 4116},
                                        {"name": "医学", "value": 4116},
                                        {"name": "体育", "value": 4116},
                                        {"name": "护理", "value": 4116},
                                        {"name": "学生", "value": 4116},
                                        {"name": "健康", "value": 4116},
                                        {"name": "学习", "value": 4116},
                                        {"name": "学员", "value": 4116},
                                        {"name": "必须", "value": 4116},
                                        {"name": "临床", "value": 4116},
                                        {"name": "课堂", "value": 4116},
                                    ]
                                },
                            ]
                        },
                        {"name": "诗派", "value": 1041},
                        {"name": "太极拳", "value": 5176},
                        {"name": "茶艺", "value": 449},
                        {"name": "陶瓷", "value": 5593},
                    ]
                },
                {
                    "name": "道德",
                    "children": [
                        {
                            "name": "规范",
                            "children": [
                                {"name": "制定", "value": 721},
                                {"name": "热心", "value": 294},
                                {"name": "义务", "value": 800},
                            ]
                        },
                        {"name": "优秀", "value": 1759},
                        {"name": "理论", "value": 2165},
                        {"name": "传统美德", "value": 586},
                    ]
                },
                {
                    "name": "多媒体",
                    "children": [
                        {"name": "计算机", "value": 6533},
                        {"name": "信息化", "value": 1732},
                        {"name": "可靠", "value": 623},
                        {"name": "快速", "value": 566}
                    ]
                },
                {
                    "name": "网络",
                    "children": [
                        {"name": "课堂", "value": 4116},
                        {"name": "上课", "value": 316},
                        {"name": "教育", "value": 2116},
                        {"name": "乡村", "value": 616},
                        {"name": "远程", "value": 316},
                        {"name": "计算机", "children": [
                            {"name": "计算机", "value": 4116},
                            {"name": "英语", "value": 4116},
                            {"name": "语言", "value": 4116},
                            {"name": "数据", "value": 4116},
                            {"name": "课程", "value": 4116},
                            {"name": "使用", "value": 4116},
                            {"name": "学习", "value": 4116},
                            {"name": "程序设计", "value": 4116},
                            {"name": "软件", "value": 4116},
                            {"name": "计算", "value": 4116},
                        ]}
                    ]
                },
                {
                    "name": "中国教育",
                    "children": [
                        {"name": "中国", "value": 4116},
                        {"name": "思想", "value": 316},
                        {"name": "政治", "value": 2116},
                        {"name": "文化", "value": 616},
                        {"name": "教育部", "value": 316},
                        {"name": "精神", "value": 316},
                        {"name": "历史", "value": 316},
                        {"name": "职业", "value": 316},
                        {"name": "全国", "value": 316},
                        {"name": "教育部", "value": 316},
                    ]
                },
                {
                    "name": "医学健康",
                    "children": [
                        {"name": "教学", "value": 4116},
                        {"name": "医学", "value": 4116},
                        {"name": "体育", "value": 4116},
                        {"name": "护理", "value": 4116},
                        {"name": "学生", "value": 4116},
                        {"name": "健康", "value": 4116},
                        {"name": "学习", "value": 4116},
                        {"name": "学员", "value": 4116},
                        {"name": "必须", "value": 4116},
                        {"name": "临床", "value": 4116},
                        {"name": "课堂", "value": 4116},
                    ]
                },
                {
                    "name": "高校",
                    "children": [
                        {"name": "高校", "value": 4116},
                        {"name": "普通高校", "value": 4116},
                        {"name": "素质教育", "value": 4116},
                        {"name": "社会主义", "value": 4116},
                        {"name": "和谐", "value": 4116},
                        {"name": "大学生", "value": 4116},
                        {"name": "教学管理", "value": 4116},
                        {"name": "高等教育", "value": 4116},
                        {"name": "必须", "value": 4116},
                    ]
                },
                {
                    "name": "技能",
                    "children": [
                        {"name": "系统", "value": 4116},
                        {"name": "技术", "value": 4116},
                        {"name": "控制", "value": 4116},
                        {"name": "旅游", "value": 4116},
                        {"name": "风险", "value": 4116},
                        {"name": "数字", "value": 4116},
                        {"name": "单片机", "value": 4116},
                        {"name": "投资", "value": 4116},
                        {"name": "原理", "value": 4116},
                        {"name": "通信", "value": 4116},
                    ]
                },
                {
                    "name": "公开课",
                    "children": [
                        {"name": "公开课", "value": 4116},
                        {"name": "网易", "value": 4116},
                        {"name": "美国", "value": 4116},
                        {"name": "纪录片", "value": 4116},
                        {"name": "故事", "value": 4116},
                        {"name": "演讲", "value": 4116},
                        {"name": "ted", "value": 4116},
                        {"name": "翻译", "value": 4116},
                        {"name": "世界", "value": 4116},
                        {"name": "公司", "value": 4116},
                    ]
                },
                {
                    "name": "公开课",
                    "children": [
                        {"name": "公开课", "value": 4116},
                        {"name": "网易", "value": 4116},
                        {"name": "美国", "value": 4116},
                        {"name": "纪录片", "value": 4116},
                        {"name": "故事", "value": 4116},
                        {"name": "演讲", "value": 4116},
                        {"name": "ted", "value": 4116},
                        {"name": "翻译", "value": 4116},
                        {"name": "世界", "value": 4116},
                        {"name": "公司", "value": 4116},
                    ]
                },
                {
                    "name": "计算机",
                    "children": [
                        {"name": "计算机", "value": 4116},
                        {"name": "英语", "value": 4116},
                        {"name": "语言", "value": 4116},
                        {"name": "数据", "value": 4116},
                        {"name": "课程", "value": 4116},
                        {"name": "使用", "value": 4116},
                        {"name": "学习", "value": 4116},
                        {"name": "程序设计", "value": 4116},
                        {"name": "软件", "value": 4116},
                        {"name": "计算", "value": 4116},
                    ]
                },
            ]
        }
        relation_data["children"] = random.sample(relation_data['children'], 5)

        # Sunburst旭日图
        sunburst_data = [
            {"name": "教育", "itemStyle": {"normal": {"color": "#FF7853"}}, "children": [
            {"name": "学习",
             "children": [{"name": "5☆", "children": [{"name": "学习"}, {"name": "诗派"}]},
                          {"name": "4☆",
                           "children": [{"name": "茶艺"}, {"name": "英语口语"}, {"name": "太极拳"}]},
                          {"name": "3☆", "children": [{"name": "教育资源"}]}]}, {"name": "成人教育", "children": [
                {"name": "5☆", "children": [{"name": "管理"}]},
                {"name": "4☆", "children": [{"name": "社会"}, {"name": "课程"}]},
                {"name": "3☆", "children": [{"name": "国防教育"}]}]}]},
                         {"name": "道德", "itemStyle": {"color": "#EA5151"}, "children": [{"name": "规范",
                                                                                         "children": [
                                                                                             {"name": "5☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "制定"}]},
                                                                                             {"name": "4☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "热心"},
                                                                                                  {
                                                                                                      "name": "义务"}]},
                                                                                             {"name": "3☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "回报"}]}]},
                                                                                        {"name": "社科",
                                                                                         "children": [
                                                                                             {"name": "5☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "优秀"}]},
                                                                                             {"name": "4☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "理论"},
                                                                                                  {
                                                                                                      "name": "进化"},
                                                                                              ]},
                                                                                             {"name": "3☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "你我他"}]}]},
                                                                                        {"name": "传统美德",
                                                                                         "children": [
                                                                                             {"name": "5☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "礼貌"}]},
                                                                                             {"name": "4☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "行为"},
                                                                                                  {
                                                                                                      "name": "进退"}]},
                                                                                             {
                                                                                                 "name": "3☆"},
                                                                                             {"name": "2☆",
                                                                                              "children": [
                                                                                                  {
                                                                                                      "name": "据报道"}]}]},
                                                                                        ]}]
        return {
            "word_cloud": word_cloud_data,
            "bar": bar_data,
            "line": line_data,
            "pie": pie_data,
            "tree": relation_data,
            "sunburst": sunburst_data,
            "text_data": {
                "max_date": max_date,
                "max_value": max_value,
                "average_value": average_value,
                "sum_value": sum_value,
                "max_word": max_word,
                "max_word_value": max_word_value,
                "select_keywords": select_keywords
            }
        }


    def get_exec_data(self, request, task_id):
        """
        获取task任务报告的数据信息
        :param request:
        :param task_id:
        :return:
        """
        try:
            if not task_id:
                return JsonResponse({'code': 400, "message": "获取参数失败，检查task_id"}, status=400)
            # 首先读取相关配置组成最终的配置参数
            try:
                task = Task.objects.get(pk=task_id)
                task_info = TaskListSerializers(task).data
            except Task.DoesNotExist as e:
                return JsonResponse({'code': 400, "message": "没有对应的任务信息"}, status=404)
            try:
                task_detail = TaskReport.objects.get(task_id=task_id)
                self.serializer_class = TaskReportSerializers
                detail_serializer = self.get_serializer(task_detail).data
                # 分内容获取数据
                charts_data = {
                    "word_cloud": json.loads(detail_serializer['word_cloud']),
                    "bar": json.loads(detail_serializer['bar_data']),
                    "line": json.loads(detail_serializer['line_data']),
                    "pie": json.loads(detail_serializer['pie_data']),
                    "tree": json.loads(detail_serializer['relation_data']),
                    "sunburst": json.loads(detail_serializer['sunburst_data']),
                    "text_data": json.loads(detail_serializer['text_data']),
                }
            except TaskReport.DoesNotExist as e:
                charts_data = self.get_random_data()
                task_instance = Task.objects.get(pk=task_id)
                # 然后是继续填充text_data数据
                if task_instance.data_source.data_source_type != 'MONGODB':
                    charts_data['text_data'][
                        'data_source_name'] = task_instance.data_source.data_source_type
                else:
                    charts_data['text_data'][
                        'data_source_name'] = task_instance.data_source.data_source_type + '-' + task_instance.data_source.db_host
                charts_data['text_data']['all_count'] = '21313'
                charts_data['text_data']['ml_type'] = task_instance.ml_config.ml_type
                charts_data['text_data']['vectorizer_type'] = task_instance.ml_config.transform_type
                if task_instance.output_data_source.output_type == 'console':
                    charts_data['text_data']['out_data_source'] = task_instance.output_data_source.output_type + '控制台输出'
                else:
                    charts_data['text_data'][
                        'out_data_source'] = task_instance.output_data_source.output_type + '-' + task_instance.output_data_source.database_type
                charts_data['text_data']['task_start_time'] = task_instance.add_datetime.strftime("%Y-%m-%d")

                # 将数据写入数据库
                task_report = TaskReport()
                task_report.bar_data = json.dumps(charts_data['bar'])
                task_report.task_id = task_instance
                task_report.word_cloud = json.dumps(charts_data['word_cloud'])
                task_report.line_data = json.dumps(charts_data['line'])
                task_report.pie_data = json.dumps(charts_data['pie'])
                task_report.relation_data = json.dumps(charts_data['tree'])
                task_report.sunburst_data = json.dumps(charts_data['sunburst'])
                task_report.text_data = json.dumps(charts_data['text_data'])
                task_report.save()
            if charts_data:
                return JsonResponse({"code": 200, "message": "获取图表数据成功",
                                     "task_data": task_info,
                                     "charts_data": charts_data}, status=200)
            else:
                return JsonResponse({"code": 404, "message": "获取task信息失败"}, status=404)
            # except Exception as e:
            #     return JsonResponse({"code": 404, "message": "获取task信息失败"}, status=404)
        except Exception as e:
            return JsonResponse({"code": 500, "message": str(e)}, status=500)



    def get_report_info(self, request, task_id):
        """
        获取关键词报告信息
        :param request:
        :return:
        """
        try:
            if not task_id:
                return JsonResponse({'code': 400, "message": "获取参数失败，检查task_id"}, status=400)
            # 首先读取相关配置组成最终的配置参数
            try:
                report = Report.objects.get(task_id=task_id)
                report_info = ReportSerializers(report).data
                report_data = json.loads(report_info['report_info'])
                if report_data:
                    return JsonResponse({"code": 200, "message": "获取图表数据成功",
                                         "task_data": report_info,
                                         "charts_data": report_data}, status=200)
                else:
                    return JsonResponse({"code": 404, "message": "获取Report信息失败"}, status=404)
            except Report.DoesNotExist as e:
                return JsonResponse({'code': 400, "message": "没有对应的任务信息"}, status=404)
        except Exception as e:
            return JsonResponse({"code": 500, "message": str(e)}, status=500)
